package com.ys.peony.ui.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.ys.peony.R;
import com.ys.peony.model.BusiTypeFileModel;
import com.ys.peony.ui.adapter.PicTypeAdapter;
import com.ys.peony.utils.PixelUtils;

import java.util.List;

/**
 * 图片类型
 */
public class PicTypePicker extends PopupWindow {
    private Context context;
    private View rootView;
    private PicTypeCallBack callBack;

    private ListView optionLv;

    //listview 列表
    private List<BusiTypeFileModel> options;
    private PicTypeAdapter adapter;

    public PicTypePicker(Context context, PicTypeCallBack callBack) {
        super();
        this.context = context;
        this.callBack = callBack;
        initView();
        initRegistClickListener();
    }


    private void initView() {
        rootView = LayoutInflater.from(context).inflate(R.layout.pop_pictypeval_layout, null);
        setContentView(rootView);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0x00ffffff);
        setBackgroundDrawable(dw);
        setOutsideTouchable(true);
        optionLv = (ListView) rootView.findViewById(R.id.option_lv);
        adapter=new PicTypeAdapter(context,options);
        optionLv.setAdapter(adapter);
    }

    public interface PicTypeCallBack {
        void picTypeItemOnclick(BusiTypeFileModel model);
    }

    private void initRegistClickListener() {
        optionLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(callBack!=null){
                    callBack.picTypeItemOnclick(options.get(position));
                }
                dismiss();
            }
        });
    }

    public void show(View mRootView,List<BusiTypeFileModel> options) {
        this.options=options;
        adapter.setData(options);
        //修改界面展示
        showAsDropDown(mRootView, 0, PixelUtils.dip2px(10, context));
    }
}
